package com.glowdayz.glowmee.bc;

import java.io.File;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.common.DateTime;
import com.glowdayz.common.MultipartBox;
import com.glowdayz.common.PageHandler;
import com.glowdayz.common.Util;
import com.glowdayz.common.MultipartBox.FileInfo;
import com.glowdayz.glowmee.cmd.PouchBannerCMD;
import com.glowdayz.glowmee.dao.CommonCodeDAO;
import com.glowdayz.glowmee.dao.PouchBannerDAO;

public class EditerPickBannerBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
    
    public EditerPickBannerBC() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		int id = Integer.parseInt(request.getParameter("id"));
		String pouchTitle = request.getParameter("name");
		pouchTitle = Util.getInstance().incoding(pouchTitle);
		
		String page_str = request.getParameter("page");
		int page = 0; 
		if (page_str == null) {
			page = 1;
		} else {
			page = Integer.parseInt(page_str);
		}
		
		try {
			request.setAttribute("id", id);
			request.setAttribute("pouchTitle", pouchTitle);
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("eventType", CommonCodeDAO.getInstance().getCommonCodeList(null, "parentId=3"));
			request.setAttribute("pouchBanner", PouchBannerDAO.getInstance().getPouchBannerList(page, 10, "p.idPouch=b.idPouch AND b.idPouch=" + id));
			request.setAttribute("display", DBHandler.getInstance().getCount("pouchbanner", "idPouchBanner", "idPouch=" + id + " AND isDisplay=1"));
			request.setAttribute("unDisplay", DBHandler.getInstance().getCount("pouchbanner", "idPouchBanner", "idPouch=" + id + " AND isDisplay=0"));
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/pouch/editerPickBanner.jsp");
		dpr.forward(request, response);
	}
	
	/** 추가와 수정을 처리하는 메서드 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		MultipartBox mbox = MultipartBox.getInstance();
		FileInfo finfo = mbox.upload2(request);
		
		int id = mbox.getInt("id");
		String pouchTitle = mbox.getValue("name");
		
		if (mbox.getValue("action_type").equals("insert")) {
			int insertSortKey = mbox.getInt("insertSortKey");
			String insertBannerTitle = mbox.getValue("insertBannerTitle");
			int insertLinkType = mbox.getInt("insertLinkType");
			String insertLinkCode = mbox.getValue("insertLinkCode");
			
			try {
				PouchBannerDAO.getInstance().insertPouchBanner(new PouchBannerCMD(id, insertBannerTitle, insertLinkType,
						insertLinkCode, insertSortKey, finfo));
				request.setAttribute("alert", "<script type='text/javascript'>alert('정상적으로 추가되었습니다.')</script>");
			} catch (Exception e) {
				DBHandler.getInstance().disconnect();
				mbox.deleteFile(finfo);
				e.printStackTrace();
			}
		} else if (mbox.getValue("action_type").equals("update")) {
			int idPouchBanner = mbox.getInt("idPouchBanner");
			int display = mbox.getInt("display" + idPouchBanner);
			if (display == -1) display = 0;
			int sortKey = mbox.getInt("sortKey" + idPouchBanner);
			String bannerTitle = mbox.getValue("bannerTitle" + idPouchBanner);
			int linkType = mbox.getInt("linkType" + idPouchBanner);
			String linkCode = mbox.getValue("linkCode" + idPouchBanner);
			
			try {
				FileInfo image = PouchBannerDAO.getInstance().getImage(idPouchBanner);
				if (image != null && finfo != null) {
					DBHandler.getInstance().beginTrans();
					String edit = "UPDATE attachefile SET fileOrgName=?, fileSaveName=?, fileDir=?, fileSize=?, fileType=?, "
							+ "modified_date=? WHERE idAttacheFile=?";
					PreparedStatement pstmt = DBHandler.getInstance().getPrepareStatement(edit);
					pstmt.setString(1, finfo.getFileOrgName());
					pstmt.setString(2, finfo.getFileSaveName());
					pstmt.setString(3, finfo.getFileDir());
					pstmt.setLong(4, finfo.getFileSize());
					pstmt.setString(5, finfo.getFileType());
					pstmt.setString(6, DateTime.getInstance().getNowDateTimeString());
					pstmt.setInt(7, image.getIdAttacheFile());
					pstmt.executeUpdate();
					pstmt.close();
					pstmt = null;
					finfo = null;
					File f = new File(image.getFileDir() + "/" + image.getFileSaveName());
					if (f.exists()) {
						f.delete();
					}
					DBHandler.getInstance().commit();
				}
			} catch (Exception e) {
				DBHandler.getInstance().rollback();
				DBHandler.getInstance().disconnect();
				mbox.deleteFile(finfo);
				e.printStackTrace();
			}
			
			
			try {
				PouchBannerDAO.getInstance().updatePouchBanner(new PouchBannerCMD(
						idPouchBanner, bannerTitle, linkType, linkCode, sortKey, display, finfo));
				request.setAttribute("alert", "<script type='text/javascript'>alert('정상적으로 수정되었습니다.')</script>");
			} catch (SQLException e) {
				DBHandler.getInstance().rollback();
				DBHandler.getInstance().disconnect();
				mbox.deleteFile(finfo);
				e.printStackTrace();
			}
		}
		
		int page = mbox.getInt("page");
		if (page == -1) page=1;
		
		try {
			request.setAttribute("id", id);
			request.setAttribute("pouchTitle", pouchTitle);
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("eventType", CommonCodeDAO.getInstance().getCommonCodeList(null, "parentId=3"));
			request.setAttribute("pouchBanner", PouchBannerDAO.getInstance().getPouchBannerList(page, 10, "p.idPouch=b.idPouch AND b.idPouch=" + id));
			request.setAttribute("display", DBHandler.getInstance().getCount("pouchbanner", "idPouchBanner", "idPouch=" + id + " AND isDisplay=1"));
			request.setAttribute("unDisplay", DBHandler.getInstance().getCount("pouchbanner", "idPouchBanner", "idPouch=" + id + " AND isDisplay=0"));
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/pouch/editerPickBanner.jsp");
		dpr.forward(request, response);
	}

}
